package com.ruoyi.system.service.impl.financial;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.db.financial.FinancialEconomic;
import com.ruoyi.system.domain.dto.FinancialEconomicDto;
import com.ruoyi.system.domain.vo.DateInfo;
import com.ruoyi.system.domain.vo.FinancialEconomicVo;
import com.ruoyi.system.domain.vo.MonthInfo;
import com.ruoyi.system.domain.vo.MonthTypeInfo;
import com.ruoyi.system.mapper.financial.FinancialEconomicMapper;
import com.ruoyi.system.service.IFinancialEconomicService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class FinancialEconomicServiceImpl implements IFinancialEconomicService {

    @Resource
    private FinancialEconomicMapper financialEconomicMapper;


    @Override
    public FinancialEconomicVo selectFinancialEconomicByOrderId(Long orderId) {
        FinancialEconomicVo financialEconomicVo = new FinancialEconomicVo();
        if (orderId != null) {
            FinancialEconomicDto financialEconomicDto = financialEconomicMapper.selectFinancialEconomicByOrderId(orderId);
            financialEconomicVo = dtoToVo(financialEconomicDto);
        }
        return financialEconomicVo;
    }

    @Override
    public List<FinancialEconomicVo> selectFinancialEconomicList(FinancialEconomic financialEconomic) {
        List<FinancialEconomicDto> financialEconomicDtos = financialEconomicMapper.selectFinancialEconomicList(financialEconomic);
        return financialEconomicDtos.stream().map(this::dtoToVo).collect(Collectors.toList());
    }

    @Override
    public int insertFinancialEconomic(FinancialEconomic financialEconomic) {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date nowDate = DateUtils.getNowDate();
//        financialEconomic.setCreateBy(loginUser.getUser().getUserId());
        financialEconomic.setCreateTime(nowDate);
        return financialEconomicMapper.insertFinancialEconomic(financialEconomic);
    }

    @Override
    public int updateFinancialEconomic(FinancialEconomic financialEconomic) {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date nowDate = DateUtils.getNowDate();
        financialEconomic.setUpdateTime(nowDate);
        return financialEconomicMapper.updateFinancialEconomic(financialEconomic);
    }

    @Override
    public int deleteFinancialEconomicByOrderIds(List<Long> orderIds) {
        return financialEconomicMapper.deleteFinancialEconomicByOrderIds(orderIds);
    }

    @Override
    public int deleteFinancialEconomicByOrderId(Long orderId) {
        return financialEconomicMapper.deleteFinancialEconomicByOrderId(orderId);
    }

    @Override
    public FinancialEconomic voToDb(FinancialEconomicVo vo) {
        FinancialEconomic result = new FinancialEconomic();
        result.setBillId(vo.getBillId());
        result.setBillTypeId(vo.getBillTypeId());
        result.setBillDetailId(vo.getBillDetailId());
        result.setSalary(vo.getSalary());
        result.setInfo(vo.getInfo());
        result.setCreateTime(vo.getCreateTime());
        result.setUpdateTime(vo.getUpdateTime());
        return result;
    }

    /**
     * 通过 orderId 和 billTypeId 确定 billId
     * @param orderId
     * @param billTypeId
     * @return
     */
    @Override
    public FinancialEconomic getBillByOrderIdAndBillTypeId(Long orderId, Integer billTypeId) {
        FinancialEconomic financialEconomic = new FinancialEconomic();
        financialEconomic.setBillDetailId(orderId);
        financialEconomic.setBillTypeId(billTypeId);
        financialEconomic = voToDb(selectFinancialEconomicList(financialEconomic).get(0));
        return financialEconomic;
    }

    private FinancialEconomicVo dtoToVo(FinancialEconomicDto dto){
        FinancialEconomicVo vo = new FinancialEconomicVo();
        vo.setBillId(dto.getBillId());
        vo.setBillTypeId(dto.getBillTypeId());
        vo.setBillDetailId(dto.getBillDetailId());
        vo.setSalary(dto.getSalary());
        vo.setInfo(dto.getInfo());
        vo.setCreateTime(dto.getCreateTime());
        vo.setUpdateTime(dto.getUpdateTime());
        return vo;
    }

    /**
     * 数组转列表List
     * @param array
     * @return
     * @param <T>
     */
    public <T> List<T> arrayToList(T[] array){
        List<T> list = new ArrayList<>();
        for (T value : array) {
            list.add(value);
        }
        return list;
    }

    @Override
    public Map<String, List<MonthInfo>> getMonthInfo(Date date) {
        List<MonthInfo> monthInfoPositive = financialEconomicMapper.getMonthInfoPositive(date);
        List<MonthInfo> monthInfoNegative = financialEconomicMapper.getMonthInfoNegative(date);
        Map<String, List<MonthInfo>> map = new HashMap<>();
        map.put("income", monthInfoPositive);
        map.put("expense", monthInfoNegative);
        return map;
    }

    @Override
    public Map<String, List<MonthTypeInfo>> getMonthTypeInfo(Date date) {
        List<MonthTypeInfo> monthTypeInfoPositive = financialEconomicMapper.getMonthTypeInfoPositive(date);
        List<MonthTypeInfo> monthTypeInfoNegative = financialEconomicMapper.getMonthTypeInfoNegative(date);
        Map<String, List<MonthTypeInfo>> map = new HashMap<>();
        map.put("income", monthTypeInfoPositive);
        map.put("expense", monthTypeInfoNegative);
        return map;
    }

    @Override
    public DateInfo getDateInfo(Date date) {
        return financialEconomicMapper.getDateInfo(date);
    }
}
